package com.ty.mianshiya.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ty.mianshiya.annotation.AuthCheck;
import com.ty.mianshiya.common.ErrorCode;
import com.ty.mianshiya.exception.BusinessException;
import com.ty.mianshiya.exception.ThrowUtils;
import com.ty.mianshiya.model.dto.questionBank.QuestionBankQueryRequest;
import com.ty.mianshiya.model.entity.Question;
import com.ty.mianshiya.model.entity.QuestionBank;
import com.ty.mianshiya.model.entity.QuestionBankQuestion;
import com.ty.mianshiya.model.entity.User;
import com.ty.mianshiya.model.vo.QuestionBankVO;
import com.ty.mianshiya.service.QuestionBankQuestionService;
import com.ty.mianshiya.service.QuestionBankService;
import com.ty.mianshiya.mapper.QuestionBankMapper;
import com.ty.mianshiya.service.QuestionService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.Count;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author WangJiyou
 * @description 针对表【question_bank(题库)】的数据库操作Service实现
 * @createDate 2024-09-09 22:35:58
 */
@Service
public class QuestionBankServiceImpl extends ServiceImpl<QuestionBankMapper, QuestionBank>
        implements QuestionBankService {

    @Override
    public void validQuestionBank(QuestionBank questionBank, boolean b) {

    }

    @Override
    public QuestionBankVO getQuestionBankVO(QuestionBank questionBank, HttpServletRequest request) {
        QuestionBankVO questionBankVO = new QuestionBankVO();
        BeanUtils.copyProperties(questionBank, questionBankVO);
        return questionBankVO;
    }

    @Override
    public Wrapper<QuestionBank> getQueryWrapper(QuestionBankQueryRequest questionBankQueryRequest) {
        if (questionBankQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        String title = questionBankQueryRequest.getTitle();
        String description = questionBankQueryRequest.getDescription();
        QueryWrapper<QuestionBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(title), "title", title);
        queryWrapper.like(StringUtils.isNotBlank(description), "description", description);
        return queryWrapper;
    }

    @Override
    public Page<QuestionBankVO> getQuestionBankVOPage(Page<QuestionBank> questionBankPage, HttpServletRequest request) {
        Page<QuestionBankVO> page = new Page<>();
        List<QuestionBankVO> records = questionBankPage.getRecords().stream().map(record -> {
            return QuestionBankVO.objToVo(record);
        }).collect(Collectors.toList());
        BeanUtils.copyProperties(questionBankPage, page);
        page.setRecords(records);
        return page;
    }


}




